﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;

namespace PersonaLibrary.Controls
{
	[TemplatePart(Name = "BackButton", Type = typeof(Button))]
	[TemplatePart(Name = "CollectionPresenter", Type = typeof(ListBox))]
	[TemplatePart(Name = "ContentPresenter", Type = typeof(ListBox))]
	class ListBoxWithContentItem : Control
	{
		private Button _backButton;
		private ListBox _collectionPresenter;
		private ContentControl _contentPresenter;

		public static readonly DependencyProperty CollectionProperty = DependencyProperty.Register("Collection",
		                                                                                           typeof (ListCollectionView),
		                                                                                           typeof (ListBoxWithContentItem),
																								   new PropertyMetadata(CollectionPropertyChanged));

		public static readonly DependencyProperty SelectedItemProperty = DependencyProperty.Register("SelectedItem",
																								   typeof(object),
																								   typeof(ListBoxWithContentItem),
																								   new PropertyMetadata(SelectedItemPropertyChanged));

		public static readonly DependencyProperty SelectedItemTemplateProperty = DependencyProperty.Register("SelectedItemTemplate",
																								   typeof(DataTemplate),
																								   typeof(ListBoxWithContentItem));

		public static readonly DependencyProperty IsItemSelectedProperty = DependencyProperty.Register("IsItemSelected",
																								   typeof(bool),
																								   typeof(ListBoxWithContentItem));

		private static void SelectedItemPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
		{
			((ListBoxWithContentItem) sender).IsItemSelected = args.NewValue != null;
			if(args.NewValue != null)
			{
				if (Application.Current.Resources.Contains(string.Concat(args.NewValue.GetType().Name, "Full")))
				{
					((ListBoxWithContentItem) sender).SelectedItemTemplate =
						Application.Current.Resources[string.Concat(args.NewValue.GetType().Name, "Full")] as DataTemplate;
				}
			}
			else
				((ListBoxWithContentItem) sender).SelectedItemTemplate = null;
		}

		private static void CollectionPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
		{
			if (args.NewValue is ListCollectionViewWrapper)
				((ListCollectionViewWrapper)args.NewValue).Refreshed += ((ListBoxWithContentItem) sender).ListBoxWithContentItemRefreshed;
		}

		public ListBoxWithContentItem()
		{
			DefaultStyleKey = typeof (ListBoxWithContentItem);
		}

		public ListCollectionView Collection 
		{ 
			set { SetValue(CollectionProperty, value); }
			get { return (ListCollectionView)GetValue(CollectionProperty); }
		}

		void ListBoxWithContentItemRefreshed()
		{
			if(IsItemSelected)
			{
				if (Application.Current.Resources.Contains(string.Concat(SelectedItem.GetType().Name, "Full")))
				{
					SelectedItemTemplate = null;
					SelectedItemTemplate = Application.Current.Resources[string.Concat(SelectedItem.GetType().Name, "Full")] as DataTemplate;
				}
			}
		}

		public object SelectedItem
		{
			set { SetValue(SelectedItemProperty, value); }
			get { return GetValue(SelectedItemProperty); }
		}

		public DataTemplate SelectedItemTemplate
		{
			set
			{
				SetValue(SelectedItemTemplateProperty, value);
				IsItemSelected = value != null;
			}
			get { return (DataTemplate)GetValue(SelectedItemTemplateProperty); }
		}

		public bool IsItemSelected
		{
			private set { SetValue(IsItemSelectedProperty, value); }
			get { return (bool)GetValue(IsItemSelectedProperty); }
		}

		public override void OnApplyTemplate()
		{
			_backButton = (Button)GetTemplateChild("BackButton");
			if (_backButton != null) 
				_backButton.Click += BackButtonClick;
			else 
				throw new Exception("Can't find button with id 'BackButton' in template");

			_collectionPresenter = (ListBox) GetTemplateChild("CollectionPresenter");
			Binding selectedItemBinding = new Binding("SelectedItem") {Source = this, Mode =  BindingMode.TwoWay};
			_collectionPresenter.SetBinding(Selector.SelectedItemProperty, selectedItemBinding);

			_contentPresenter = (ContentControl)GetTemplateChild("ContentPresenter");
			Binding selectedItemTemplateBinding = new Binding("SelectedItemTemplate") { Source = this, Mode = BindingMode.TwoWay };
			_contentPresenter.SetBinding(ContentControl.ContentTemplateProperty, selectedItemTemplateBinding);

			base.OnApplyTemplate();
			
		}

		void BackButtonClick(object sender, RoutedEventArgs e)
		{
			SelectedItem = null;
			IsItemSelected = false;
		}
	}
}
